package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.omg.CORBA.StringHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;


    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save (@RequestBody SetmealDto setmealDto){

        log.info("套餐信息：{}",setmealDto);

        setmealService.saveWithDish(setmealDto);

        return R.success("新增菜品成功");
    }


    /**
     * 套餐的分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page (int page,int pageSize,String name) {

        //分页构造对象
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> dtoPage = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件查询，根据name进行like模糊查询
        queryWrapper.like(name!=null,Setmeal::getName,name);
        //添加排序条件，根据更新的时间排序
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        //重新组装对象，对象拷贝
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");//分页参数信息
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> list= records.stream().map((item)->{
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category byId = categoryService.getById(categoryId);
            if(byId!=null){
                setmealDto.setCategoryName(byId.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(list);
        return R.success(dtoPage);
    }


    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete (@RequestParam List<Long> ids){
        log.info("ids:{}",ids);
        setmealService.deleteWithDish(ids);
        return R.success("套餐数据删除成功");
    }


    /**
     * 根据条件查询套餐数据
     * @return
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(queryWrapper);
        return R.success(list);
    }

    /**
     * 套餐的停售起售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> updateS1 (@RequestParam List<String> ids){
        for (String id : ids) {
            //获取到的id字符串转换成Long类型
            long l = Long.parseLong(id);
            //获取到id的t套餐
            Setmeal byId = setmealService.getById(l);
            //讲套餐的类型修改
            byId.setStatus(0);
            //通过id修改套餐的状态
            setmealService.updateById(byId);
        }
        return R.success("修改成功");
    }


    /**
     * 套餐的停售起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> updateS2 (@RequestParam List<String> ids){
        for (String id : ids) {
            //获取到的id字符串转换成Long类型
            long l = Long.parseLong(id);
            //获取到id的套餐
            Setmeal byId = setmealService.getById(l);
            //将套餐的类型修改
            byId.setStatus(1);
            //通过id修改套餐的状态
            setmealService.updateById(byId);

        }
   /*     //获取当前用户id
        Long currentId = BaseContext.getCurrentId();

        //查询菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getStatus,currentId);
        List<Dish> list = dishService.list(queryWrapper);
        if (list.size()==0) {
            throw new CustomException("菜品停售，不能修改");
        }     */

        return R.success("修改成功");
    }


    /**
     * 修改套餐
     * @return
     */
    @GetMapping("/{id}")
    public R<SetmealDto> getId(@PathVariable Long id){
        //查询菜品基本信息，从dish表中查询
        Setmeal getid = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(getid,setmealDto);
        //查询当前套餐中的菜品信息。从setmeal_dish中查询
        LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> list = setmealDishService.list(queryWrapper1);
        setmealDto.setSetmealDishes(list);


        return R.success(setmealDto);
    }
    @GetMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWith(setmealDto);
        return R.success("");
    }
}

